package com.da_client_rn.bridge;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

public class CustomFrameLayout extends SimpleViewManager<FrameLayout> {

    private static final String REACT_CLASS = "CustomFrameLayout";
    private FrameLayout floatingView; // 这里定义全局变量，避免重复创建

    @NonNull
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @NonNull
    @Override
    protected FrameLayout createViewInstance(@NonNull ThemedReactContext themedReactContext) {
        if (floatingView == null) { // 避免重复创建
            floatingView = new FrameLayout(themedReactContext);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT
            );
            params.gravity = Gravity.BOTTOM;
            floatingView.setLayoutParams(params);
        }

        if (!Settings.canDrawOverlays(themedReactContext)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            themedReactContext.getApplicationContext().startActivity(intent);
            Toast.makeText(themedReactContext, "需要悬浮窗权限，请授予权限", Toast.LENGTH_SHORT).show();
        } else {

            // 设置背景样式
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(Color.RED);
            drawable.setCornerRadius(30);
            floatingView.setBackground(drawable);


            TextView textView = new TextView(themedReactContext);
            textView.setText("hello，我是信息流广告");
            if(textView.getParent() != null ) {
                floatingView.removeView(textView);
            }
            floatingView.addView(textView);

        }

        return floatingView;
    }

    @ReactProp(name = "backgroundColor")
    public void setBackgroundColor(FrameLayout view, String color) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(Color.parseColor(color));
        view.setBackground(drawable);
    }

    @ReactProp(name = "cornerRadius")
    public void setCornerRadius(FrameLayout view, float radius) {
        GradientDrawable drawable = (GradientDrawable) view.getBackground();
        if (drawable != null) {
            drawable.setCornerRadius(radius);
        }
    }

    @Override
    protected void onAfterUpdateTransaction(@NonNull FrameLayout view) {
        super.onAfterUpdateTransaction(view);
        Log.d("CustomFrameLayout", "FrameLayoutViewManager 组件已挂载并更新");
    }

    @Override
    public void onDropViewInstance(@NonNull FrameLayout view) {
        super.onDropViewInstance(view);
        Log.d("CustomFrameLayout", "FrameLayoutViewManager 组件已被卸载");
    }
}
